package com.supor.aiot.common.utils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.android.baseconfig.common.utils.GsonUtil;
import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.ToastUtils;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.entity.BannerInfo;
import com.supor.aiot.common.entity.DeviceInfo;
import com.supor.aiot.common.helper.GlobalDataCache;
import com.supor.aiot.common.ui.activity.WebActivity;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * <p>  可加多个内链参数，多个参数以&符连接，tinyId 为小程序 id，url 为页面地址
 * <p>     食谱详情内链：{recipeDetail:tinyId= xxxx&url= xxxx&id=xxxx&type=xxxxx}
 * <p>     单个活动内链：{activityName:tinyId= xxxx&url= xxxx&id=xxxx&type=xxxxx}
 * <p>     轮播图内链：{banner:tinyId= xxxx&url= xxxx&id=xxxx&type=xxxxx}
 * <p>     设备更多食谱内链：{deviceRecipeList:tinyId= xxxx&url= xxxx&id=xxxx&type=xxxxx}
 * <p>     食谱分类内链：{recipeCategory:tinyId= xxxx&url= xxxx&id=xxxx&type=xxxxx}
 * <p>     设备详情页内链：{deviceDetail:tinyId= xxxx&url= xxxx&id=xxxx&type=xxxxx}
 * <p>     食谱专辑内链：{recipeAlbum:tinyId= xxxx&url= xxxx&id=xxxx&type=xxxxx}
 * <p>     运营专辑内链：{activityAlbum:tinyId= xxxx&url= xxxx&id=xxxx&type=xxxxx}
 * 描述: BannerUtils <br>
 * 作者: lei <br>
 * 日期: 2021/10/22 <br>
 */
public class BannerUtils {
    /** 食谱详情内链 */
    private static final String RECIPE_DETAIL = "recipeDetail:";
    /** 单个活动内链 */
    private static final String ACTIVITY_NAME = "activityName:";
    /** 轮播图内链 */
    private static final String BANNER = "banner:";
    /** 设备更多食谱内链 */
    private static final String DEVICE_RECIPE_LIST = "deviceRecipeList:";
    /** 食谱分类内链 */
    private static final String RECIPE_CATEGORY = "recipeCategory:";
    /** 设备详情页内链 */
    private static final String DEVICE_DETAIL = "deviceDetail:";
    /** 食谱专辑内链 */
    private static final String RECIPE_ALBUM = "recipeAlbum:";
    /** 运营专辑内链 */
    private static final String ACTIVITY_ALBUM = "activityAlbum:";

    public static void jump(Activity activity, BannerInfo info) {
        String path = info.linkUrl;
        Logc.d("banner path: " + path);
        if (TextUtils.isEmpty(path)) {
            return;
        }
        if (path.length() < 2) {
            return;
        }
        path = path.trim();
        if (info.adTypeStatus == 1) {
            Intent intent = new Intent(activity, WebActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt(Constants.Key.param_type, 1);
            bundle.putString(Constants.Key.param_url, path);
            if (!TextUtils.isEmpty(info.adName)) {
                bundle.putString(Constants.Key.param_title, info.adName);
            }
            intent.putExtras(bundle);
            activity.startActivity(intent);
            return;
        }
        BannerParam param;
        path = path.substring(1, path.length() - 1);
        if (path.startsWith(RECIPE_DETAIL)) {
            param = new BannerParam(path.substring(RECIPE_DETAIL.length()));
            if (TextUtils.isEmpty(param.tinyId)) {
                if (TextUtils.isEmpty(param.params)) {
                    param.params = "id=" + param.id;
                }
                AppAgent.startRecipePage(param.params);
            } else {
                if (TextUtils.isEmpty(param.url)) {
                    param.url = "pages/index/index";
                }
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, param.url, "", param.params);
                } else {
                    AppAgent.startTiny(param.tinyId, param.url, param.params);
                }
            }
        } else if (path.startsWith(ACTIVITY_NAME)) {
            param = new BannerParam(path.substring(ACTIVITY_NAME.length()));
            if (TextUtils.isEmpty(param.tinyId)) {
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, Constants.App.PAGE_AT_HOME, "", "");
                } else {
                    AppAgent.startAtHomePage(param.params);
                }
            } else {
                if (TextUtils.isEmpty(param.url)) {
                    param.url = "pages/index/index";
                }
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, param.url, "", param.params);
                } else {
                    AppAgent.startTiny(param.tinyId, param.url, param.params);
                }
            }
        } else if (path.startsWith(BANNER)) {
            param = new BannerParam(path.substring(BANNER.length()));
            if (TextUtils.isEmpty(param.tinyId)) {
                if ("home".equalsIgnoreCase(param.type)) {
                    if (AppAgent.h5Debug) {
                        AppAgent.startDebugTiny(activity, Constants.App.PAGE_AT_HOME, "", "");
                    } else {
                        AppAgent.startAtHomePage(param.params);
                    }
                }
            } else {
                if (TextUtils.isEmpty(param.url)) {
                    param.url = "pages/index/index";
                }
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, param.url, "", param.params);
                } else {
                    AppAgent.startTiny(param.tinyId, param.url, param.params);
                }
            }
        } else if (path.startsWith(DEVICE_RECIPE_LIST)) {
            param = new BannerParam(path.substring(DEVICE_RECIPE_LIST.length()));
            if (TextUtils.isEmpty(param.tinyId)) {
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, Constants.App.PAGE_MORE_RECIPE, "", param.params);
                } else {
                    AppAgent.startMoreRecipePage(param.params);
                }
            } else {
                if (TextUtils.isEmpty(param.url)) {
                    param.url = "pages/index/index";
                }
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, param.url, "", param.params);
                } else {
                    AppAgent.startTiny(param.tinyId, param.url, param.params);
                }
            }
        } else if (path.startsWith(RECIPE_CATEGORY)) {
            param = new BannerParam(path.substring(RECIPE_CATEGORY.length()));
            if (TextUtils.isEmpty(param.tinyId)) {
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, Constants.App.PAGE_AT_HOME, "", "");
                } else {
                    AppAgent.startAtHomePage(param.params);
                }
            } else {
                if (TextUtils.isEmpty(param.url)) {
                    param.url = "pages/index/index";
                }
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, param.url, "", param.params);
                } else {
                    AppAgent.startTiny(param.tinyId, param.url, param.params);
                }
            }
        } else if (path.startsWith(DEVICE_DETAIL)) {
            param = new BannerParam(path.substring(DEVICE_DETAIL.length()));
            if (!AppAgent.isLogin()) {
                ToastUtils.getInstance().showCustomToast("您还未登录");
                return;
            }
            if (TextUtils.isEmpty(param.tinyId)) {
                if (GlobalDataCache.deviceTree != null && GlobalDataCache.deviceTree.device != null) {
                    for (DeviceInfo deviceInfo : GlobalDataCache.deviceTree.device) {
                        if (deviceInfo.model.equalsIgnoreCase(param.id)) {
                            JSONObject jo =JSONObject.parseObject(GsonUtil.getInstance().toJson(deviceInfo));
                            jo.put("deviceData", jo.getString("originalData"));
                            jo.remove("originalData");
                            Logc.d("device: " + jo.toJSONString());
                            if (Constants.Type.TYPE_CHEF_MACHINE.equalsIgnoreCase(deviceInfo.type)) {
                                AppAgent.startDevicePage(Constants.App.TINY_DEVICE_C,
                                        jo.toJSONString());
                            } else if (Constants.Type.TYPE_OVEN.equalsIgnoreCase(deviceInfo.type)) {
                                AppAgent.startDevicePage(Constants.App.TINY_DEVICE_OVEN,
                                        jo.toJSONString());
                            }
                            return;
                        }
                    }
                }
            } else {
                if (TextUtils.isEmpty(param.url)) {
                    param.url = "pages/index/index";
                }
                if (GlobalDataCache.deviceTree != null && GlobalDataCache.deviceTree.device != null) {
                    for (DeviceInfo deviceInfo : GlobalDataCache.deviceTree.device) {
                        if (deviceInfo.model.equalsIgnoreCase(param.id)) {
                            JSONObject jo =JSONObject.parseObject(GsonUtil.getInstance().toJson(deviceInfo));
                            jo.put("deviceData", jo.getString("originalData"));
                            jo.remove("originalData");
                            Logc.d("device: " + jo.toJSONString());
                            AppAgent.startTiny(param.tinyId, param.url, jo.toJSONString(), param.params);
                            return;
                        }
                    }
                }
            }
        } else if (path.startsWith(RECIPE_ALBUM)) {
            param = new BannerParam(path.substring(RECIPE_ALBUM.length()));
            if (TextUtils.isEmpty(param.tinyId)) {
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, Constants.App.PAGE_AT_HOME, "", "");
                } else {
                    AppAgent.startAtHomePage(param.params);
                }
            } else {
                if (TextUtils.isEmpty(param.url)) {
                    param.url = "pages/index/index";
                }
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, param.url, "", param.params);
                } else {
                    AppAgent.startTiny(param.tinyId, param.url, param.params);
                }
            }
        } else if (path.startsWith(ACTIVITY_ALBUM)) {
            param = new BannerParam(path.substring(ACTIVITY_ALBUM.length()));
            if (TextUtils.isEmpty(param.tinyId)) {
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, Constants.App.PAGE_AT_HOME, "", "");
                } else {
                    AppAgent.startAtHomePage(param.params);
                }
            } else {
                if (TextUtils.isEmpty(param.url)) {
                    param.url = "pages/index/index";
                }
                if (AppAgent.h5Debug) {
                    AppAgent.startDebugTiny(activity, param.url, "", param.params);
                } else {
                    AppAgent.startTiny(param.tinyId, param.url, param.params);
                }
            }
        }
    }

    static class BannerParam {
        public String tinyId;
        public String url;
        public String id;
        public String type;
        public String params;

        public BannerParam(String param) {
            String [] split = param.split("&");
            for (String string : split) {
                String[] strings = string.split("=");
                if (strings.length < 2) {
                    continue;
                }
                String value = strings[0];
                if (value == null) {
                    continue;
                }
                value = value.trim();
                if ("id".equalsIgnoreCase(value)) {
                    id = strings[1].trim();
                } else if ("type".equalsIgnoreCase(value)) {
                    type = strings[1].trim();
                } else if ("tinyId".equalsIgnoreCase(value)) {
                    tinyId = strings[1].trim();
                } else if ("url".equalsIgnoreCase(value)) {
                    url = strings[1].trim();
                }
            }
            if (param.contains("&id=")) {
                params = param.substring(param.indexOf("&id=") + 1).trim();
            }
        }
    }
}
